package guilayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import modellayer.Product;
import controllayer.ProductCtrl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductOverview extends JPanel {
	private ProductCtrl productCtrl;
	private JTable table;
	private DefaultTableModel tableModel;
	private String tableSelectedProductBarcode;
	private JButton btnLookupProduct;
	private JButton btnEditProduct;
	private JButton btnDeleteProduct;

	/**
	 * Create the panel.
	 */
	public ProductOverview() {
		productCtrl = ProductCtrl.getInstance();
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		createAndSetTable(scrollPane);
		
		createAndSetBottomButtons();
		
		showBottomButtons(false);

	}

	private void createAndSetBottomButtons() {
		JPanel panel = new JPanel();
		// Remove the background
		panel.setOpaque(false);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);
		
		btnLookupProduct = new JButton("Se produkt");
		btnLookupProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(2, 22, tableSelectedProductBarcode);
			}
		});
		panel.add(btnLookupProduct);
		
		btnEditProduct = new JButton("Rediger produkt");
		btnEditProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(2, 23, tableSelectedProductBarcode);
			}
		});
		panel.add(btnEditProduct);
		
		btnDeleteProduct = new JButton("Slet produkt");
		btnDeleteProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//callRootPane(1, 14, tableSelectedCustomerNo);
				buttonActionDeleteProduct();
			}
		});
		panel.add(btnDeleteProduct);
	}

	private void createAndSetTable(JScrollPane scrollPane) {
		String[] columnNames = { "Produkt navn", "Pris", "Stregkode" };
		tableModel = new DefaultTableModel(null, columnNames);
		
		for(Product product : productCtrl.getProductList()) {
			
			String name = product.getProductLabel();
			double dPrice = product.getPrice();
			String barcode = product.getBarcode();
			
			String price = Double.toString(dPrice);
			
			String[] rowData = { name, price, barcode };
			
			tableModel.addRow(rowData);
		}
		
		table = new JTable(tableModel) {  
				public boolean isCellEditable(int row,int column){  
					return false;  
				}
			};
			
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				JTable target = (JTable)e.getSource();
		         
		         int row = target.getSelectedRow();
		         
		         tableSelectedProductBarcode = (String) table.getValueAt(row, 2); // Getting the data which is in the "row" number and column 0 (the first).
		         
		         showBottomButtons(true);
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			      if (e.getClickCount() == 2) {
			         JTable target = (JTable)e.getSource();
			         
			         int row = target.getSelectedRow();
			         
			         String productNo = (String) table.getValueAt(row, 2); // Getting the data which is in the "row" number and column 0 (the first).
			         
			         callRootPane(2, 22, productNo);
			      }
			   }
			});
		
		scrollPane.setViewportView(table);
	}
	
	private void showBottomButtons(boolean show) {
		btnLookupProduct.setVisible(show);
		btnEditProduct.setVisible(show);
		btnDeleteProduct.setVisible(show);
	}
	
	private void buttonActionDeleteProduct() {
		callRootPane(2, 24, tableSelectedProductBarcode);
	}

	
	private void callRootPane(int leftMenu, int mainContent, Object data) {
		((MainWindow)(this.getRootPane().getParent())).refreshAll(leftMenu, mainContent, data);
	}

}
